//
//  MyArticleViewController.m
//  MyProject
//
//  Created by dllo on 15/11/3.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "MyArticleViewController.h"
#import "MyButifulGoodsWithZTableViewCell.h"
#import "DataBase.h"
#import "ButifulArticle.h"
#import "UIImageView+WebCache.h"
#import "ElephantWebViewController.h"

@interface MyArticleViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableview;
@property (nonatomic,strong) NSMutableArray *array;
@end

@implementation MyArticleViewController

- (void)viewWillAppear:(BOOL)animated
{
    DataBase *data = [DataBase dataBase];
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"ButifulArticle"];
    NSArray *arr = [data.managedObjectContext executeFetchRequest:request error:nil];
    
    self.array = [NSMutableArray array];
    for (ButifulArticle *collect in arr) {
        [self.array addObject:collect];
    }
    
    if (self.array.count == 0) {
        UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"提示" message:@"暂时没有收藏,快去添加吧..." preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alertView animated:YES completion:^{
            
        }];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        }];
        [alertView addAction:action];
    }

    
    [self.tableview reloadData];
    
    if (![[NSUserDefaults standardUserDefaults]boolForKey:@"isWhite"]) {
        self.tableview.backgroundColor = [UIColor grayColor];
    }
    else{
        self.tableview.backgroundColor = [UIColor whiteColor];
    }

    
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"美文收藏";

    // Do any additional setup after loading the view from its nib.
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    
    [self.tableview registerNib:[UINib nibWithNibName:@"MyButifulGoodsWithZTableViewCell" bundle:nil] forCellReuseIdentifier:@"name"];
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.editButtonItem.title = @"编辑";
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.view.bounds.size.height/3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyButifulGoodsWithZTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"name"];
  ButifulArticle *buti = [self.array objectAtIndex:indexPath.row];
    cell.titleLabel.text = buti.title;
    [cell.myImage sd_setImageWithURL:[NSURL URLWithString:buti.name] placeholderImage:[UIImage imageNamed:@"lovebeijing.png"]];
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ButifulArticle *buti = [self.array objectAtIndex:indexPath.row];

    ElephantWebViewController *ele = [[ElephantWebViewController alloc]init];
    [self.navigationController pushViewController:ele animated:YES];
    ele.url = buti.url;
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    if (self.editing) {
        self.editButtonItem.title = @"完成";
    }
    else{
        self.editButtonItem.title = @"编辑";
    }
    [self.tableview setEditing:editing animated:animated];
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;

}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        DataBase *data = [DataBase dataBase];
        [data.managedObjectContext deleteObject:[self.array objectAtIndex:indexPath.row]];
        [data saveContext];
        [self.array removeObjectAtIndex:indexPath.row];
        
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationBottom];
        [self.tableview reloadData];
        if (self.array.count == 0) {
            UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"提示" message:@"暂时没有收藏,快去添加吧..." preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alertView animated:YES completion:^{
                
            }];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            
            [alertView addAction:action];
            
        }
        
    }
    
    
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
